on: workflow_dispatch

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

name: release
permissions:
  contents: read

env:
  flutter_version: "3.10.x"

jobs:
  calc-release-version:
    name: calc-release-version
    runs-on: ubuntu-latest
    steps:
      - name: checkout code
        uses: actions/checkout@v3
        with:
          # safety github token credential
          persist-credentials: false
          fetch-depth: 0
      - name: override releaserc config
        run: cat .releaserc-pubspec > .releaserc
      - name: calculate release version
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: "npm"
      - run: npm ci
      - run: npx semantic-release
        env:
          # only for read
          GITHUB_TOKEN: ${{ secrets.CI_RELEASE_TOKEN }}
      - name: version
        run: cat pubspec.yaml | grep version
      - name: upload pubspec with new version
        uses: actions/upload-artifact@v3
        with:
          name: pubspec
          path: pubspec.yaml
  build-linux:
    name: build-linux
    runs-on: ubuntu-latest
    needs: calc-release-version
    steps:
      - name: checkout code
        uses: actions/checkout@v3
      - name: download release pubspec
        uses: actions/download-artifact@v3
        with:
          name: pubspec
      - name: version
        run: cat pubspec.yaml | grep version
      - name: set up flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.flutter_version }}
          channel: "stable"
          cache: true
          cache-key: "flutter-:os:-:channel:-:version:-:arch:-:hash:" # optional, change this to force refresh cache
          cache-path: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:" # optional, change this to specify the cache path
      - name: apt install build deps
        uses: awalsh128/cache-apt-pkgs-action@latest
        with:
          packages: zip ninja-build libgtk-3-dev
      - name: flutter enable linux desktop target
        run: flutter config --enable-linux-desktop
      - name: flutter build linux
        run: flutter build linux
      - name: zip build linux
        uses: thedoctor0/zip-release@0.7.1
        with:
          type: zip
          filename: linux-x64.zip
          directory: build/linux/x64/release/bundle
      - name: upload linux build artifact
        uses: actions/upload-artifact@v3
        with:
          name: linux-x64
          path: build/linux/x64/release/bundle/linux-x64.zip
  build-windows:
    name: build-windows
    runs-on: windows-latest
    needs: calc-release-version
    steps:
      - name: checkout code
        uses: actions/checkout@v3
      - name: download release pubspec
        uses: actions/download-artifact@v3
        with:
          name: pubspec
      - name: set up flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.flutter_version }}
          channel: "stable"
          cache: true
          cache-key: "flutter-:os:-:channel:-:version:-:arch:-:hash:" # optional, change this to force refresh cache
          cache-path: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:" # optional, change this to specify the cache path
      - name: flutter enable windows desktop target
        run: flutter config --enable-windows-desktop
      - name: flutter build windows
        run: flutter build windows
      - name: zip build windows
        uses: thedoctor0/zip-release@0.7.1
        with:
          type: zip
          filename: windows-x64.zip
          directory: build/windows/runner/Release
      - name: upload windows build artifact
        uses: actions/upload-artifact@v3
        with:
          name: windows-x64
          path: build/windows/runner/Release/windows-x64.zip
  build-macos:
    name: build-macos
    runs-on: macos-latest
    needs: calc-release-version
    steps:
      - name: checkout code
        uses: actions/checkout@v3
      - name: download release pubspec
        uses: actions/download-artifact@v3
        with:
          name: pubspec
      - name: set up dmg-packer
        run: brew install create-dmg
      - name: set up flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.flutter_version }}
          channel: "stable"
          cache: true
          cache-key: "flutter-:os:-:channel:-:version:-:arch:-:hash:" # optional, change this to force refresh cache
          cache-path: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:" # optional, change this to specify the cache path
      - name: flutter enable macos desktop target
        run: flutter config --enable-macos-desktop
      - name: flutter build macos
        run: flutter build macos --no-tree-shake-icons
      - name: build macos dmg
        run: create-dmg --volname "Pilot S3" --window-pos 200 120 --window-size 800 529 --app-drop-link 540 250 --hdiutil-quiet --app-drop-link 600 185 "pilot-s3.dmg" "build/macos/Build/Products/Release/pilot_s3.app/"
      - name: upload macos build artifact
        uses: actions/upload-artifact@v3
        with:
          name: macos-x64
          path: pilot-s3.dmg
  build-android:
    name: build-android
    runs-on: ubuntu-latest
    needs: calc-release-version
    steps:
      - name: checkout code
        uses: actions/checkout@v3
      - name: download release pubspec
        uses: actions/download-artifact@v3
        with:
          name: pubspec
      - name: set up flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.flutter_version }}
          channel: "stable"
          cache: true
          cache-key: "flutter-:os:-:channel:-:version:-:arch:-:hash:" # optional, change this to force refresh cache
          cache-path: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:" # optional, change this to specify the cache path
      - name: flutter build android
        run: flutter build apk --no-tree-shake-icons
      - name: move build android
        run: mv build/app/outputs/flutter-apk/app-release.apk pilot-s3.apk
      - name: upload android build artifact
        uses: actions/upload-artifact@v3
        with:
          name: android-x64
          path: pilot-s3.apk
  build-web:
    name: build-web
    runs-on: ubuntu-latest
    needs: calc-release-version
    steps:
      - name: checkout code
        uses: actions/checkout@v3
      - name: download release pubspec
        uses: actions/download-artifact@v3
        with:
          name: pubspec
      - name: set up flutter
        uses: subosito/flutter-action@v2
        with:
          flutter-version: ${{ env.flutter_version }}
          channel: "stable"
          cache: true
          cache-key: "flutter-:os:-:channel:-:version:-:arch:-:hash:" # optional, change this to force refresh cache
          cache-path: "${{ runner.tool_cache }}/flutter/:channel:-:version:-:arch:" # optional, change this to specify the cache path
      - name: flutter build web
        run: flutter build web --no-tree-shake-icons
      - name: upload to s3
        id: s3
        uses: shallwefootball/s3-upload-action@master
        with:
          aws_key_id: ${{ secrets.S3_ACCESS_KEY }}
          aws_secret_access_key: ${{ secrets.S3_SECRET_KEY}}
          aws_bucket: ${{ secrets.S3_BUCKET }}
          endpoint: ${{ vars.S3_ENDPOINT }}
          source_dir: build/web
          destination_dir: ""
  github-release:
    name: github-release
    runs-on: ubuntu-latest
    needs:
      [
        calc-release-version,
        build-linux,
        build-windows,
        build-macos,
        build-android,
      ]
    steps:
      - name: checkout code
        uses: actions/checkout@v3
        with:
          # safety github token credential
          persist-credentials: false
          fetch-depth: 0
      - name: download release pubspec
        uses: actions/download-artifact@v3
        with:
          name: pubspec
      - name: version
        run: cat pubspec.yaml | grep version
      - name: download linux build artifact
        uses: actions/download-artifact@v3
        with:
          name: linux-x64
      - name: download windows build artifact
        uses: actions/download-artifact@v3
        with:
          name: windows-x64
      - name: download macos build artifact
        uses: actions/download-artifact@v3
        with:
          name: macos-x64
      - name: download android build artifact
        uses: actions/download-artifact@v3
        with:
          name: android-x64
      - name: release
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: "npm"
      - run: npm ci
      - run: npx semantic-release
        env:
          GITHUB_TOKEN: ${{ secrets.CI_RELEASE_TOKEN }}
          GIT_AUTHOR_NAME: 64mb-robot
          GIT_AUTHOR_EMAIL: robot@64mb.ru
          GIT_COMMITTER_NAME: 64mb-robot
          GIT_COMMITTER_EMAIL: robot@64mb.ru
